package com.nowcoder.community.service.impl;

import com.nowcoder.community.service.AdminService;
import com.nowcoder.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.tags.EditorAwareTag;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private RedisTemplate redisTemplate;

    // 按天生成时间
    // 不需要精确到小时
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public void recordUV(String ip) {
        String redisKey = RedisKeyUtil.getUVKey(sdf.format(new Date()));
        redisTemplate.opsForHyperLogLog().add(redisKey, ip);
    }

    @Override
    public long calculateUV(Date startDate, Date endDate) {
        if(startDate == null || endDate == null) {
            throw new IllegalArgumentException("日期参数不能为空!");
        }
        // 整理当前日期范围内的key
        List<String> dateList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        while(!calendar.getTime().after(endDate)) {
            String tmpRedisKey = RedisKeyUtil.getUVKey(sdf.format(calendar.getTime()));
            dateList.add(tmpRedisKey);
            // 在calendar中加一天
            calendar.add(Calendar.DATE, 1);
        }

        String redisKey = RedisKeyUtil.getUVKey(sdf.format(startDate), sdf.format(endDate));
        redisTemplate.opsForHyperLogLog().union(redisKey, dateList.toArray());

        // 返回统计结果
        return redisTemplate.opsForHyperLogLog().size(redisKey);
    }

    @Override
    public void recordDAU(int userId) {
        String redisKey = RedisKeyUtil.getDAUKey(sdf.format(new Date()));
        redisTemplate.opsForValue().setBit(redisKey, userId, true);
    }

    @Override
    public long calculateDAU(Date startDate, Date endDate) {
        if(startDate == null || endDate == null) {
            throw new IllegalArgumentException("日期参数不能为空!");
        }
        List<byte[]> dateList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        while(!calendar.getTime().after(endDate)) {
            String tmpRedisKey = RedisKeyUtil.getDAUKey(sdf.format(calendar.getTime()));
            dateList.add(tmpRedisKey.getBytes());
            // 在calendar中加一天
            calendar.add(Calendar.DATE, 1);
        }
        return (long) redisTemplate.execute(new RedisCallback() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                String redisKey = RedisKeyUtil.getDAUKey(sdf.format(startDate), sdf.format(endDate));
                connection.bitOp(RedisStringCommands.BitOperation.OR, redisKey.getBytes(), dateList.toArray(new byte[0][0]));
                return connection.bitCount(redisKey.getBytes());
            }
        });
    }

}
